package com.ruoyi.elastic.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.elastic.service.cluster;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
//import org.elasticsearch.client.indices.IndexStats;
import javax.xml.ws.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @CreateTime: 2023-01-06  22:11
 * @Author: chaozi
 * @Version: 1.0
 */
@Service
@Slf4j
public class clusterImpl implements cluster {
    //_cluster/health
    @Autowired
    RestHighLevelClient client;
    @Autowired
    private RestTemplate restTemplate;
//    private final RestTemplate restTemplate;
//
//    public clusterImpl(RestTemplate restTemplate) {
//        this.restTemplate = restTemplate;
//    }

    /***
     *@Description: TODO 查询集群健康信息
     *@CreateTime: 2023/1/6 22:51
     *@Author: chaozi
     *@Version: 1.0
     */
    @Override
    public Map health() throws IOException {
        Map<String, Object> map = new HashMap();
        try {
            //_cluster/health?pretty=true
            ClusterHealthRequest clusterHealthRequest=new ClusterHealthRequest();
            clusterHealthRequest.indices("?pretty=true");
            ClusterHealthResponse clusterHealthResponse=client.cluster().health(clusterHealthRequest, RequestOptions.DEFAULT);
            //JsonString to map
            //转换结果集
            map = JSON.parseObject(clusterHealthResponse.toString(), new TypeReference<HashMap<String, Object>>() {});
        }catch (Exception e){
            map.put("result","clusterHealthError");
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    /***
     *@Description: TODO
     * GET _cat/indices?format=json&h=health,status,index,id,shardsPrimary,shardsReplica,docsCount,docsDeleted,storeSize,creation.date.string
     *@CreateTime: 2023/1/6 23:17
     *@Author: chaozi
     *@Version: 1.0
     */
    @Override
    public Map indices() {

        GetIndexRequest request = new GetIndexRequest();

        IndicesClient indicesClient=client.indices();
       Object obj= restTemplate.getForObject("",Object.class);
        ResponseEntity responseEntity= new ResponseEntity<Object>(obj, HttpStatus.OK);
       System.out.println(responseEntity.toString());
        return null;
    }


}
